Crate vrd

source ·
Expand description

Random (VRD) 🦀

vrd

A Rust library for generating high-quality random numbers based on the Mersenne Twister algorithm.

Part of the Mini Functions family of libraries.

Crates.io Lib.rs License Rust

Overview

The Mersenne Twister is a pseudorandom number generator (PRNG) that is often used in computer simulations and games. It is a fast and reliable PRNG.

Random (VRD) can be used to generate random numbers based on the Mersenne Twister algorithm. The library generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) starting from any odd seed in 0..(2^32 - 1). The index is incremented after each random number is generated. When the index reaches 624, the array is reinitialized and the index is reset to 0.

Features ✨

  • Create new random number generator and seed it with a value.
  • Design for speed and efficiency in mind.
  • Generate random 32-bit unsigned integer within a given range.
  • Provide random numbers of different types, including booleans, bytes, chars, doubles, floats, integers, and unsigned integers.
  • Mutate the state of the random number generator.
  • Produce pseudo-random number sequences that are different from each other.
  • Regulate the randomness of the generated numbers, including the seed value and the number of bits used.
  • Select a random element from a slice of values.

Usage

Add the following to your Cargo.toml file:

[dependencies]
vrd = "0.0.5"
serde = { version = "1.0.160", features = ["derive"] }

Examples

Check out the examples folder for helpful snippets of code that demonstrate how to use the Random (VRD) library. You can also check out the documentation for more information on how to use the library.

use vrd::Random;

let mut rng = Random::new();
let rand_int = rng.int(1, 10);
println!("Random integer between 1 and 10: {}", rand_int);

let rand_float = rng.float();
println!("Random float: {}", rand_float);

let rand_bytes = rng.bytes(10);
println!("Random bytes: {:?}", rand_bytes);

Errors

The Random struct methods do not currently return errors, but in the future they may return Result types to handle situations where the Mersenne Twister algorithm fails to generate a random number or a range of numbers.

License

The project is licensed under the terms of both the MIT license and the Apache License (Version 2.0).

Re-exports

Modules

  • The macros module contains functions for generating macros.
  • The mersenne_twister module contains the implementation of the Mersenne Twister algorithm.

Macros

  • Generate a random boolean with a provided probability.
  • Generate a vector of random bytes with the provided length using the provided Random (VRD) struct
  • Generate a random char within the range ‘a’..=‘z’ using the provided Random (VRD) struct
  • Generate a random element from a slice of values using the provided Random (VRD) struct
  • Generate a random double using the provided Random (VRD) struct
  • Generate a random float using the provided Random (VRD) struct
  • Generate a random integer within the given range using the provided Random (VRD) struct
  • Generate a new random number
  • Generate a random 32-bit unsigned integer using the provided Random (VRD) struct
  • Generate a random number within the given range using the provided Random (VRD) struct
  • Seed the provided Random (VRD) struct with the given value
  • Twist the state of the provided Random (VRD) struct
  • Generate a random 32-bit unsigned integer within the given range using the provided Random (VRD) struct
  • Generate a random 32-bit unsigned integer within the given range using the provided Random (VRD) struct

Structs

  • The Random struct is used to generate random numbers using the Mersenne Twister algorithm.

Functions

  • The main entry point for the Random (VRD) library.